#ifndef MatrixClock_h
#define MatrixClock_h

#include<LedControl.h>
#define maxDevices 4
#define INTERVEL 50


const static byte num[10][8] PROGMEM={
  {0b11110000,0b10010000,0b10010000,0b10010000,0b10010000,0b10010000,0b11110000,0b00000000},  //0
  {0b00100000,0b01100000,0b00100000,0b00100000,0b00100000,0b00100000,0b01110000,0b00000000},  //1
  {0b11110000,0b00010000,0b00010000,0b11110000,0b10000000,0b10000000,0b11110000,0b00000000},  //2
  {0b11110000,0b00010000,0b00010000,0b11110000,0b00010000,0b00010000,0b11110000,0b00000000},  //3
  {0b10010000,0b10010000,0b10010000,0b11110000,0b00010000,0b00010000,0b00010000,0b00000000},  //4
  {0b11110000,0b10000000,0b10000000,0b11110000,0b00010000,0b00010000,0b11110000,0b00000000},  //5
  {0b11110000,0b10000000,0b10000000,0b11110000,0b10010000,0b10010000,0b11110000,0b00000000},  //6
  {0b11110000,0b00010000,0b00010000,0b00010000,0b00010000,0b00010000,0b00010000,0b00000000},  //7
  {0b11110000,0b10010000,0b10010000,0b11110000,0b10010000,0b10010000,0b11110000,0b00000000},  //8
  {0b11110000,0b10010000,0b10010000,0b11110000,0b00010000,0b00010000,0b11110000,0b00000000}   //9
};
const static byte snum[10][8] PROGMEM = {
  {0x00,0x00,0xE0,0xA0,0xA0,0xA0,0xE0,0x00},//0
  {0x00,0x00,0x40,0xC0,0x40,0x40,0xE0,0x00},//1
  {0x00,0x00,0xE0,0x20,0xE0,0x80,0xE0,0x00},//2
  {0x00,0x00,0xE0,0x20,0xE0,0x20,0xE0,0x00},//3
  {0x00,0x00,0xA0,0xA0,0xE0,0x20,0x20,0x00},//4
  {0x00,0x00,0xE0,0x80,0xE0,0x20,0xE0,0x00},//5
  {0x00,0x00,0xE0,0x80,0xE0,0xA0,0xE0,0x00},//6
  {0x00,0x00,0xE0,0x20,0x20,0x20,0x20,0x00},//7
  {0x00,0x00,0xE0,0xA0,0xE0,0xA0,0xE0,0x00},//8
  {0x00,0x00,0xE0,0xA0,0xE0,0x20,0xE0,0x00},//9
};
/*
const static byte charweek[][8] PROGMEM = {

  {0x7E,0x18,0x18,0x18,0x18,0x18,0x18,0x00}, //"T"
  {0x42,0x42,0x42,0x42,0x42,0x42,0x3C,0x00}, //"U"
  {0x7E,0x40,0x40,0x7C,0x40,0x40,0x7E,0x00}, //"E"


  {0x42,0x66,0x66,0x5A,0x5A,0x42,0x42,0x00}, //"M"
  {0x18,0x24,0x42,0x42,0x42,0x24,0x18,0x00},//"O"
  {0x42,0x62,0x72,0x5A,0x4E,0x46,0x42,0x00}, //"N"

  {0x3C,0x62,0x30,0x18,0x0C,0x46,0x3C,0x00}, //"S"
  {0x18,0x3C,0x24,0x3C,0x66,0x42,0x42,0x00}, //"A"
  {0x7E,0x18,0x18,0x18,0x18,0x18,0x18,0x00}, //"T"

  {0x42,0x5A,0x5A,0x7E,0x3C,0x18,0x18,0x00}, //"W"
  {0x7E,0x40,0x40,0x7C,0x40,0x40,0x7E,0x00}, //"E"
  {0x78,0x4C,0x46,0x42,0x46,0x4C,0x78,0x00}, //"D"

  {0x7E,0x18,0x18,0x18,0x18,0x18,0x18,0x00}, //"T"
  {0x42,0x42,0x42,0x7E,0x42,0x42,0x42,0x00}, //"H"
  {0x42,0x42,0x42,0x42,0x42,0x42,0x3C,0x00}, //"U"

  {0x7E,0x40,0x40,0x7C,0x40,0x40,0x40,0x00}, //"F"
  {0x7C,0x42,0x42,0x7C,0x58,0x4C,0x46,0x00}, //"R"
  {0x3C,0x18,0x18,0x18,0x18,0x18,0x3C,0x00}, //"I"

  {0x3C,0x62,0x30,0x18,0x0C,0x46,0x3C,0x00}, //"S"
  {0x42,0x42,0x42,0x42,0x42,0x42,0x3C,0x00}, //"U"
  {0x42,0x62,0x72,0x5A,0x4E,0x46,0x42,0x00}  //"N"

};
*/

const static unsigned long charweeks[][8] PROGMEM = {
  {0x003C4242,0x00624262,0x00304272,0x0018425A,0x000C424E,0x00464246,0x003C3C42,0x00000000},  //SUN
  {0x00421842,0x00662462,0x00664272,0x005A425A,0x005A424E,0x00422446,0x00421842,0x00000000},  //MON
  {0x007E427E,0x00184240,0x00184240,0x0018427C,0x00184240,0x00184240,0x00183C7E,0x00000000},  //TUE
  {0x00427E78,0x005A404C,0x005A4046,0x007E7C42,0x003C4046,0x0018404C,0x00187E78,0x00000000},  //WED
  {0x007E4242,0x00184242,0x00184242,0x00187E42,0x00184242,0x00184242,0x0018423C,0x00000000},  //THU
  {0x007E7C3C,0x00404218,0x00404218,0x007C7C18,0x00405818,0x00404C18,0x0040463C,0x00000000},  //FRI
  {0x003C187E,0x00623C18,0x00302418,0x00183C18,0x000c6618,0x00464218,0x003C4218,0x00000000}   //SAT
 
};

const static unsigned long charwifi[8] PROGMEM = {
  0x00000000,
  0x42BE8000,
  0x5A200000,
  0x7EBC8000,
  0x3CA08000,
  0x18A08000,
  0x18A08000,
  0x00000000
};

const static unsigned long charconnected[8] PROGMEM = {
  0x00000000,
  0x00000020,
  0x3BBBBBB8,
  0x22AAAA20,
  0x22AABA20,
  0x22AAA228,
  0x3BAABBB8,
  0x00000000
};

const static unsigned long charnotcon[8] PROGMEM = {
  0x00000000,
  0x00200000,
  0x3BB80000,
  0x2AA00000,
  0x2AA00000,
  0x2AA80000,
  0x2BB80000,
  0x00000000
};

class MatrixClock{
    private :

        /*LedControl对象  */
        LedControl lc;

        //时钟显示数组
        unsigned long matrix[8];

        //点阵显示数组
        unsigned long matrix_clock[8];
        //月日显示数组
        unsigned long matrix_ym[8];

        //星期显示数组；
        unsigned long matrix_day[8];

        /* 记录上一次Merge()的小时*/
        byte lasthour;

        /* 记录上一次Merge()的分钟*/
        byte lastminute;

        /* 记录上一次Merge()的秒*/
        byte lastsecond;

        /*  记录上一次执行RollDisplay()的系统时间*/
        unsigned long lastmillis;

        //切换年月日时的的刷新时间
        unsigned long switchtime;
        
        //切换状态标识
        byte switchflag;

        //数组切换时的列号
        byte colyear;

        //日的列号
        int count;

        /*  记录上一次RollMerge()时，更新到第几行*/
        byte row;

        /*  本次RollDisplay()时分秒六位数字是否有数字需要更新,
          *第0位表示秒的个位，
          *第1位表示秒的十位，
          *第2位表示分钟的个位，
          *依次类推......
          *第5位表示小时的十位，
          */
        int  flag;

        //显示WiFi连接过程中点的数量
        byte dots;
        
        //检查当前时间值是否变化
        byte isChanged(byte hour,byte minute,byte second); 
        
        //填充年月日数组
        void dayupdate(uint16_t year, uint8_t month, uint8_t day, uint8_t dow);

            /* 生成时分秒LED点阵数组*/
        void Merge(uint8_t hour,uint8_t minute,uint8_t second);

        /* 显示当前点阵数组*/
        void Display();

    public:
    /* 
         * 生成一个MatrixClock控制器
         * Params :
         * DIN		    Max7219的DIN引脚
         * CLK  		Max7219的CLK引脚
         * CS		Max7219的CS引脚 
         */
    MatrixClock(int DIN,int  CLK,int  CS);

    /*  MatrixClock 对象初始化*/
    void init();

    /*带时间初始化*/
    void timeinit(uint8_t hour,uint8_t minute,uint8_t second);



    /* 生成滚动效果Led点阵数组,    updown 为true时向上滚动，为false时向下滚动*/
    void RollMerge(uint8_t hour,uint8_t minute,uint8_t second,bool updown);    

    /* 每隔intervel ms的时间，显示点阵数组*/
    void Display(unsigned int intervel);

    /*  滚动显示时间*/
    void testdisplay(uint16_t year,uint8_t month,uint8_t day,uint8_t dow,uint8_t hour,uint8_t minute,uint8_t second,bool updown);

    //显示wifi的连接过程
    void wifidot();

    //显示wifi连接成功
    void wificonnected();

    void wifinotconn();
};


#endif